package com.benshou.bcss.common.db;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableLogic;
import com.baomidou.mybatisplus.annotation.Version;
import com.benshou.bcss.common.j17common.annotation.NotNull;
import com.benshou.bcss.common.lambda.Lambdas;
import com.benshou.bcss.common.tenant.TenantContext;
import com.benshou.bcss.common.user.UserContext;
import lombok.Data;

import java.time.ZonedDateTime;
import java.util.Objects;

/**
 * 数据库实体类：基类
 *
 * @author <a href="mailto:liuzhenbang@benshou.com">Liu Zhenbang</a>
 */
@Data
public class BaseEntity {

    /**
     * 用户编号：系统
     */
    public static final String USER_NO_SYSTEM = "1";

    /**
     * 用户编号：未知
     */
    public static final String USER_NO_UNKNOWN = "0";

    /**
     * 版本号的初始化值
     */
    public static final int VERSION_INITIAL = 1;

    /**
     * 公共环境租户唯一标识
     */
    public static final long TENANT_ID_PUBLIC = 0;

    /**
     * 字段名：自增主键
     */
    public static final String NAME_ID =
            Objects.requireNonNull(Lambdas.loadFieldName(BaseEntity::getId));

    /**
     * 字段名：创建者
     */
    public static final String NAME_CREATED_BY =
            Objects.requireNonNull(Lambdas.loadFieldName(BaseEntity::getCreatedBy));

    /**
     * 字段名：创建时间
     */
    public static final String NAME_CREATED_TIME =
            Objects.requireNonNull(Lambdas.loadFieldName(BaseEntity::getCreatedTime));

    /**
     * 字段名：更新人
     */
    public static final String NAME_UPDATED_BY =
            Objects.requireNonNull(Lambdas.loadFieldName(BaseEntity::getUpdatedBy));

    /**
     * 字段名：更新时间
     */
    public static final String NAME_UPDATED_TIME =
            Objects.requireNonNull(Lambdas.loadFieldName(BaseEntity::getUpdatedTime));

    /**
     * 字段名：乐观锁版本号
     */
    public static final String NAME_VERSION =
            Objects.requireNonNull(Lambdas.loadFieldName(BaseEntity::getVersion));

    /**
     * 字段名：是否已删除
     */
    public static final String NAME_IS_DELETED =
            Objects.requireNonNull(Lambdas.loadFieldName(BaseEntity::getIsDeleted));

    /**
     * 字段名：租户唯一标识
     */
    public static final String NAME_TENANT_ID =
            Objects.requireNonNull(Lambdas.loadFieldName(BaseEntity::getTenantId));

    /**
     * 字段名：租户唯一标识
     */
    public static final String COLUMN_TENANT_ID =
            DBCts.toColumnNameByFieldName(NAME_TENANT_ID);

    /**
     * 自增主键
     */
    @TableField(value = "id")
    @NotNull
    private Long id;

    /**
     * 创建者
     */
    @TableField(value = "created_by", fill = FieldFill.INSERT)
    @NotNull
    private String createdBy;

    /**
     * 创建时间
     */
    @TableField(value = "created_time", fill = FieldFill.INSERT)
    @NotNull
    private ZonedDateTime createdTime;

    /**
     * 更新者
     */
    @TableField(value = "updated_by", fill = FieldFill.INSERT_UPDATE)
    @NotNull
    private String updatedBy;

    /**
     * 更新时间
     */
    @TableField(value = "updated_time", fill = FieldFill.INSERT_UPDATE)
    @NotNull
    private ZonedDateTime updatedTime;

    /**
     * 乐观锁版本号
     */
    @TableField(value = "version", fill = FieldFill.INSERT)
    @Version
    @NotNull
    private Integer version;

    /**
     * 是否已删除
     */
    @TableField(value = "is_deleted", fill = FieldFill.INSERT)
    @TableLogic
    @NotNull
    private Boolean isDeleted;

    /**
     * 租户唯一标识
     */
    @TableField(value = "tenant_id", fill = FieldFill.INSERT)
    @NotNull
    private Long tenantId;

    /**
     * @return 当前租户 Id
     */
    public static long getCurrentTenantId() {
        return TenantContext.isPublic() ?
                TENANT_ID_PUBLIC :
                Objects.requireNonNull(TenantContext.getTenantId());
    }

    /**
     * @return 当前用户编号
     */
    public static String getCurrentUserNo() {
        var currentUserNo = UserContext.getUserNo();
        return currentUserNo == null ? USER_NO_UNKNOWN : currentUserNo;
    }
}
